package task5;

import java.util.*;

/**
 *  使用集合实现斗地主游戏的部分功能，要求如下：
 *
 *  （1）首先准备 54 张扑克牌并打乱顺序。
 *
 *  （2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。
 *
 *  （3）查看三个玩家手中的扑克牌和底牌。
 *
 *  （4）其中玩家手中的扑克牌需要按照大小顺序打印，规则如下：
 *
 *     手中扑克牌从大到小的摆放顺序：大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
 */
public class Task5Test {
    public static void main(String[] args) {
        //创建扑克数组
        List<String> poker = Arrays.asList("大王","小王","2","A","K","Q","J","10","9","8","7","6","5","4","3","2","A","K","Q","J","10","9","8","7","6","5","4","3","2","A","K","Q","J","10","9","8","7","6","5","4","3","2","A","K","Q","J","10","9","8","7","6","5","4","3");
        //洗牌
        Collections.shuffle(poker);
        List<String> playerA = new ArrayList(17);
        List<String> playerB = new ArrayList(17);
        List<String> playerC = new ArrayList(17);
       //发牌并打印
        for (int i = 0; i < poker.size()-3; i++) {
            if (i%3==0){
                playerA.add(poker.get(i));
            }
            else if (i%3==1){
                playerB.add(poker.get(i));
            }
            else playerC.add(poker.get(i));
        }
        System.out.println("发牌完毕，底牌为："+poker.get(53)+" "+poker.get(52)+" "+poker.get(51));
        System.out.println("玩家A手牌为："+playerA);
        System.out.println("玩家B手牌为："+playerB);
        System.out.println("玩家B手牌为："+playerC);
        //重新构建comparator
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //给大小王排序
                if ("大王".equals(o1)&&"小王".equals(o2)) return -1;
                if ("大王".equals(o2)&&"小王".equals(o1)) return 1;
                //给K排序
                if ("K".equals(o1)&&"Q".equals(o2)) return -1;
                if ("K".equals(o2)&&"Q".equals(o1)) return 1;
                //给A排序
                if ("A".equals(o1)&&"K".equals(o2)) return -1;
                if ("A".equals(o2)&&"K".equals(o1)) return 1;
                if ("A".equals(o1)&&"Q".equals(o2)) return -1;
                if ("A".equals(o2)&&"Q".equals(o1)) return 1;
                if ("A".equals(o1)&&"J".equals(o2)) return -1;
                if ("A".equals(o2)&&"J".equals(o1)) return 1;
                //给2排序
                if ("2".equals(o1)&&"K".equals(o2)) return -1;
                if ("2".equals(o2)&&"K".equals(o1)) return 1;
                if ("2".equals(o1)&&"Q".equals(o2)) return -1;
                if ("2".equals(o2)&&"Q".equals(o1)) return 1;
                if ("2".equals(o1)&&"J".equals(o2)) return -1;
                if ("2".equals(o2)&&"J".equals(o1)) return 1;
                if ("2".equals(o1)&&"A".equals(o2)) return -1;
                if ("2".equals(o2)&&"A".equals(o1)) return 1;
                for (int i = 3; i < 11; i++) {
                    if ("2".equals(o1) && String.valueOf(i).equals(o2)) return -1;
                    if ("2".equals(o2) && String.valueOf(i).equals(o1)) return 1;
                }
                //给10排序
                for (int i = 3; i < 10; i++) {
                    if ("10".equals(o1) && String.valueOf(i).equals(o2)) return -1;
                    if ("10".equals(o2) && String.valueOf(i).equals(o1)) return 1;
                }
                return o2.compareTo(o1);
            }
        };
        //给手牌排序并打印
        playerA.sort(comparator);
        playerB.sort(comparator);
        playerC.sort(comparator);
        System.out.println("排序后玩家A手牌为："+playerA);
        System.out.println("排序后玩家B手牌为："+playerB);
        System.out.println("排序后玩家B手牌为："+playerC);
    }
}
